import type { IRouteInfoResp } from '@/api/types'
import { useRoute } from '@/hooks/useRoute'

// 使用示例
export function useRouteExample() {
  // 基础使用 - 自动加载路由
  const {
    routes,
    loading,
    error,
    visibleRoutes,
    menuRoutes,
    findRouteByPath,
    getBreadcrumb,
    hasPermission,
  } = useRoute()

  // 查找指定路径的路由
  const findRoute = (path: string) => {
    return findRouteByPath(path)
  }

  // 获取面包屑导航
  const getPageBreadcrumb = (currentRoute: IRouteInfoResp) => {
    return getBreadcrumb(currentRoute)
  }

  // 检查权限
  const checkPermission = (permission: string) => {
    return hasPermission(permission)
  }

  return {
    routes,
    loading,
    error,
    visibleRoutes,
    menuRoutes,
    findRoute,
    getPageBreadcrumb,
    checkPermission,
  }
}

// 高级使用 - 自定义过滤和排序
export function useCustomRoute() {
  const { routes, loading, reload } = useRoute({
    immediate: true,
    filter: route => route.type === 1 && !route.isHidden, // 只显示菜单类型且非隐藏的路由
    sort: (a, b) => a.sort - b.sort, // 按排序值升序排列
  })

  return {
    routes,
    loading,
    reload,
  }
}

// 手动加载路由
export function useManualRoute() {
  const { routes, loading, loadRoutes } = useRoute({
    immediate: false, // 不自动加载
  })

  // 手动触发加载
  const loadRouteData = async () => {
    try {
      await loadRoutes()
      console.log('路由加载成功')
    }
    catch (error) {
      console.error('路由加载失败:', error)
    }
  }

  return {
    routes,
    loading,
    loadRouteData,
  }
}
